package com.virgo.demo.system.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.virgo.common.entity.IUserToken;
import com.virgo.common.exception.IApplicationException;
import com.virgo.common.utils.IUserUtils;
import com.virgo.demo.core.consts.SiteConst;
import com.virgo.demo.system.domain.AdminUserDo;
import com.virgo.demo.system.mapper.AdminUserMapper;
import com.virgo.demo.system.service.AdminLoginService;
import com.virgo.demo.system.service.AdminUserService;
import com.virgo.demo.system.service.SystemResourceService;
import com.virgo.login.service.LoginSecurityService;
import com.virgo.login.service.TokenService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;

@Service
@RequiredArgsConstructor
public class AdminLoginServiceImpl extends ServiceImpl<AdminUserMapper, AdminUserDo> implements AdminLoginService {

	private final AdminUserService adminUserService;

	private final LoginSecurityService loginSecurityService;

	private final TokenService tokenService;

	private final SystemResourceService systemResourceService;

	@Override
	public HashMap<String, Object> login(String username, String password, String ip, String userAgent) {
		String userType = SiteConst.AdminUserType;
		Boolean canLogin = loginSecurityService.checkLogin(userType, username, ip);
		if (!canLogin) {
			throw new IApplicationException("错误次数太多,请稍后再试");
		}
		boolean loginResult = true;
		AdminUserDo adminUserDo = adminUserService.loadByAdminName(username);
		if (ObjectUtils.isEmpty(adminUserDo) || adminUserDo.getStatus().equals(0)) {
			loginResult = false;
		} else {
			String inputPwd = IUserUtils.encodePassWord(password,adminUserDo.getPwdSalt());
			if (!inputPwd.equals(adminUserDo.getLoginPwd())) {
				loginResult = false;
			}
		}
		if (!loginResult) {
			String failMessage = loginSecurityService.checkSecurity(userType, username, ip);
			throw new IApplicationException(failMessage);
		}
		loginSecurityService.clearSecurity(userType, username, ip);
		IUserToken userToken = new IUserToken();
		userToken.setUserType(userType);
		userToken.setUserId(adminUserDo.getId());
		userToken.setNickName(adminUserDo.getNickName());
		userToken.setHeadIco("");
		userToken.setRoleId(adminUserDo.getRoleId());
		userToken.setIp(ip);
		userToken.setUserAgent(userAgent);
		userToken.setUnique(DigestUtil.md5Hex(userToken.getIp() + "_" + userToken.getUserAgent()));
		HashMap<String, Object> token = tokenService.createToken(userToken);
		token.put("nickName",adminUserDo.getNickName());
		token.put("menu",systemResourceService.menuData(adminUserDo.getRoleId()));
		token.put("button",systemResourceService.buttonData(adminUserDo.getRoleId()));
		adminUserService.doLoginAfter(adminUserDo,ip);
		return token;
	}

	/**
	 * 退出登录
	 */
	@Override
	public void logout() {
		tokenService.logout();
	}

	/**
	 * 修改登录密码
	 *
	 * @param oldPwd String
	 * @param pwd    String
	 */
	@Override
	public void uodateLoginPwd(String oldPwd, String pwd) {
		IUserToken iUserToken = tokenService.get(true);
		AdminUserDo adminUserDo = adminUserService.getById(iUserToken.getUserId());
		if (ObjectUtils.isEmpty(adminUserDo)) {
			throw new IApplicationException("数据异常");
		}
		String inputPwd = IUserUtils.encodePassWord(oldPwd,adminUserDo.getPwdSalt());
		if (!inputPwd.equals(adminUserDo.getLoginPwd())) {
			throw new IApplicationException("旧密码错误");
		}
		String password = IUserUtils.encodePassWord(pwd,adminUserDo.getPwdSalt());
		AdminUserDo updateItem = new AdminUserDo();
		updateItem.setId(adminUserDo.getId());
		updateItem.setLoginPwd(password);
		adminUserService.updateById(updateItem);
	}
}